<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class LianjieController extends Controller
{
    public function add()
    {
    	return view('admin.lianjie.add',['title'=>'添加链接']);
    }

    public function doadd(Request $request)
    {
    	$data = $request -> except('_token');
    	$res = DB::table('youqinglianjie') -> insert([$data]);
    	if($res)
         {
            return back() -> with(['info' => '添加成功']);
         }else
         {
            return back() -> with(['info' => '添加失败']);
         }
    }


    public function index(Request $request)
    {
    	$num = $request -> input('num',10);
        $keywords = $request -> input('keywords','');
        $data = DB::table('youqinglianjie') -> where('name','like','%'.$keywords.'%') -> paginate($num) ;


        return view('admin.lianjie.index',['data' => $data ,'request' => $request -> all(), 'title' => 'URL列表']);
    }

    public function delete($id)
    {
        $res = DB::table('youqinglianjie') -> where('id',$id) -> delete();
        if($res)
         {
            return back() -> with(['info' => '删除成功']);
         }else
         {
            return back() -> with(['info' => '删除失败']);
         }
    }

    public function edit($id)
    {
    	$list = DB::table('youqinglianjie') -> where('id',$id) -> first();
        
        return view('admin.lianjie.edit',['title'=>'编辑公告','list'=>$list]);

    }

    public function update(Request $request)
    {
    	$data = $request -> except('_token');
        $res = DB::table('youqinglianjie') -> where('id',$data['id']) -> update(['name'=>$data['name'],'url'=>$data['url']]);
        if($res)
         {
          return redirect('/admin/lianjie/index') -> with(['info' => '修改成功']);
         }else
         {
          return back() -> with(['info' => '修改失败']);
         }
    }




}
